package com.study.controller;

import com.study.entity.AliOSSProperties;
import com.study.entity.Result;
import com.study.utils.AliOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@Slf4j
@RestController
public class UploadController {
    /*  本地存储
     @PostMapping("/upload")
        public Result upload(String username, Integer age, MultipartFile file) throws Exception{
            String name = file.getOriginalFilename();
            log.info("username={},age={},file={}",username,age,name);
            String localName = UUID.randomUUID().toString() + name.substring(name.lastIndexOf("."));
            String path = "C:\Users\51230\Desktop\JAVA全栈\js\\"+localName;
            file.transferTo(new File(path));
            return Result.success(path);
        }*/



 //阿里云OSS存储


//    private String endpoint ="https://oss-cn-guangzhou.aliyuncs.com";
//    private String bucketName="boot-project1";

    //通过配置文件注入属性方式一：
/*    @Value("${aliyun.oss.endpoint}")
    private String endpoint;
    @Value("${aliyun.oss.buckedName}")
    private String bucketName;*/


    //通过配置文件注入属性方式二：
    @Autowired
    private AliOSSProperties aliOSSProperties;
    @PostMapping("/upload")
    public Result upload(MultipartFile file) throws Exception {
        String name = file.getOriginalFilename();
        log.info("file={}", name);
        String nameAfter = name.substring(name.lastIndexOf("."));
        byte[] content = file.getBytes();
        String path = AliOSSUtils.upload(aliOSSProperties.getEndpoint(), aliOSSProperties.getBuckedName(), content, nameAfter);
        return Result.success(path);
    }
}
